//package com.xjh.config;
//
//import com.xjh.service.PasswordUserDetailsService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.stereotype.Component;
//
///**
// * 密码登陆认证器
// *
// * @author XuJianHua
// * @version 1.0
// * @company 湖南安第斯信息科技有限公司, 版权所有
// * @date 2022/7/13 15:29
// */
//
///**
// * AuthenticationProvider ：
// * 认证管理的逻辑实现类，当我们需要实现自己的认证逻辑的时候需要实现此接口，
// * 重写 public Authentication authenticate(Authentication authentication) 方法；
// *
// *
// * */
//@Component
//public class PasswordAuthenticationProvider implements AuthenticationProvider {
//    @Autowired
//    private PasswordUserDetailsService passwordUserDetailsService;
//    @Autowired
//    private BCryptPasswordEncoder bCryptPasswordEncoder;
//
//
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        // 这里做的操作相当于要重写 auth.userDetailsService(userDetailsService).passwordEncoder(password())这一句
//
//        // 1.获取用户传入的userName,密码
//        String username = authentication.getName();
//        String password = authentication.getCredentials().toString();
//        // 2.获取数据库数据
//        UserDetails userDetails = passwordUserDetailsService.loadUserByUsername(username);
//        // 3.密码校验
//        if (bCryptPasswordEncoder.matches(userDetails.getPassword(), password)) {
//            // 返回 UsernamePasswordAuthenticationToken
//            return new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
//
//        }else {
//            throw new BadCredentialsException("Bad credentials");
//        }
//
//    }
//
//    @Override
//    public boolean supports(Class<?> aClass) {
//        return false;
//    }
//
//    public void setUserDetailsService(PasswordUserDetailsService passwordUserDetailsService) {
//        this.passwordUserDetailsService = passwordUserDetailsService;
//    }
//
//
//}
